package com.xbfinal.eduorder.service.impl;

import com.xbfinal.commonUtils.ordervo.CourseWebVoOrder;
import com.xbfinal.commonUtils.ordervo.UcenterMemberOrder;
import com.xbfinal.eduorder.client.EduClient;
import com.xbfinal.eduorder.client.UcenterClient;
import com.xbfinal.eduorder.entity.TOrder;
import com.xbfinal.eduorder.mapper.TOrderMapper;
import com.xbfinal.eduorder.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbfinal.eduorder.utils.OrderNoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author xbfinal
 * @since 2023-03-11
 */
@Service
@Slf4j
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String creatOrders(String id, String memberId) {

        //通过远程调用 根据用户id获取用户信息
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);

        //通过远程调用 根据课程id获取课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(id);


        TOrder tOrder = new TOrder();
        //订单号
        tOrder.setOrderNo(OrderNoUtil.getOrderNo());
        tOrder.setCourseId(id); //课程id
        tOrder.setCourseTitle(courseInfoOrder.getTitle());
        tOrder.setCourseCover(courseInfoOrder.getCover());
        tOrder.setTeacherName(courseInfoOrder.getTeacherName());
        tOrder.setTotalFee(courseInfoOrder.getPrice());
        tOrder.setMemberId(memberId);
        tOrder.setMobile(userInfoOrder.getMobile());
        tOrder.setNickname(userInfoOrder.getNickname());

        tOrder.setStatus(0);  //订单状态（0：未支付 1：已支付）
        tOrder.setPayType(1);  //支付类型 ，微信1，支付宝2

        baseMapper.insert(tOrder);

        //返回订单id
        return tOrder.getOrderNo();
    }
}
